const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;

const arr = [0, 2, 1, 1];
const simd_arr = SIMD.Int32x4(...arr);
const simd_add = SIMD.Int32x4(0, 1, 2, 3);
const int_arr = new Int32Array(arr.length);

function test_simd() {
	const simd_res = SIMD.Int32x4.add(simd_arr, simd_add);
	// return simd_res;

	// 转化为 typed array
	SIMD.Int32x4.store(int_arr, 0, simd_res);
	return int_arr;

	// 转化为 array
	// const res = Array.prototype.slice.call(int_arr);
	// return res;
}

function test_map() {
	const res = arr.map((item, i) => item + i);
	return res;
}

function test_arr() {
	const res = [];
	for (var i = 0, len = arr.length; i < len; i++) {
		res[i] = arr[i] + i
	}
	return res;
}
suite
	.add('simd', test_simd)
	.add('map', test_map)
	.add('arr', test_arr)
	.on('cycle', function(event) {
		console.log(String(event.target));
	})
	.on('complete', function() {
		console.log('Fastest is ' + this.filter('fastest').map('name'));
	})
	.run();